﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Douban.Core.Helpers
{
    public class AppFileHelper
    {
        public static string _appFileDirectory;
        public const string AppDirectoryName = "豆Q";

        static AppFileHelper()
        {
            _appFileDirectory = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 
                AppDirectoryName);

            if (!Directory.Exists(_appFileDirectory))
                Directory.CreateDirectory(_appFileDirectory);
        }

        public static string GetFullPath(string fileName)
        {
            return Path.Combine(_appFileDirectory, fileName);
        }
        
        public static Stream ReadFile(string fileName)
        {
            return File.Open(GetFullPath(fileName), FileMode.Open);
        }

        public static void Create(string fileName)
        {
            File.Create(GetFullPath(fileName)).Close();
        }

        public static bool Exists(string fileName)
        {
            return File.Exists(GetFullPath(fileName));
        }

        public static bool DirectoryExists(string directoryName)
        {
            return Directory.Exists(GetFullPath(directoryName));
        }

        public static void CreateDirectory(string directoryName)
        {
            Directory.CreateDirectory(GetFullPath(directoryName));
        }

        public static void WriteFile(string fileName, Action<Stream> writeAction)
        {
            Stream stream = File.Open(GetFullPath(fileName), FileMode.Open);
            writeAction(stream);
            stream.Close();
        }
    }
}
